package com.meida.module.pay.provider.controller;


import com.egzosn.pay.common.api.PayConfigStorage;
import com.egzosn.pay.common.bean.PayOutMessage;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.springmvc.base.BaseController;
import com.meida.module.pay.client.entity.PayConfig;
import com.meida.module.pay.provider.response.PayResponse;
import com.meida.module.pay.provider.service.PayConfigService;
import com.meida.module.pay.provider.service.PayInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;


/**
 * 订单支付
 *
 * @author flyme
 * @date 2019-06-22
 */
@RestController
@Api(tags = "支付模块-支付管理")
public class PayController extends BaseController<PayConfigService, PayConfig> {

    @Resource
    private PayConfigService service;

    @Resource
    private PayInfoService payInfoService;

    /**
     * 获取APP支付预订单信息
     *
     * @return 支付预订单信息
     * @desc 接收参数:payId:支付方式Id,outTradeNo:订单号
     */
    @ApiOperation(value = "获取APP端支付参数", notes = "获取APP支付参数")
    @PostMapping(value = "/pay/app")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "payId", value = "支付方式ID", paramType = "form"),
            @ApiImplicitParam(name = "handlerName", value = "支付handlerName", paramType = "form"),
            @ApiImplicitParam(name = "outTradeNo", value = "订单号", paramType = "form")
    })
    public ResultBody getPayInfo(@RequestParam(required = false) Map params) {
        return payInfoService.getPayInfo(params, "APP");
    }


    /**
     * 获取二维码支付图片
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "payId", value = "支付方式ID", paramType = "form"),
            @ApiImplicitParam(name = "handlerName", value = "支付handlerName", paramType = "form"),
            @ApiImplicitParam(name = "outTradeNo", value = "订单号", paramType = "form")
    })
    @ApiOperation(value = "二维码支付下单", notes = "获取二维码")
    @GetMapping(value = "/pay/qrPay")
    public ResultBody toWxQrPay(@RequestParam(required = false) Map params) {
        return payInfoService.getPayInfo(params, "NATIVE");
    }

    /**
     * 支付回调地址
     */
    @PostMapping(value = "/common/payBack/{handlerName}/{payId}")
    public String payBack(HttpServletRequest request, @PathVariable Integer payId, @PathVariable String handlerName) throws IOException {
        PayResponse payResponse = service.getPayResponse(new Long(payId), handlerName);
        PayConfigStorage storage = payResponse.getStorage();
        Map<String, Object> params = payResponse.getService().getParameter2Map(request.getParameterMap(), request.getInputStream());
        if (null == params) {
            return payResponse.getService().getPayOutMessage("fail", "失败").toMessage();
        }
        //校验
        if (payResponse.getService().verify(params)) {
            PayOutMessage outMessage = payResponse.getRouter().route(params, storage);
            return outMessage.toMessage();
        }
        return payResponse.getService().getPayOutMessage("fail", "失败").toMessage();
    }
}
